/*  Game/Army Editor for yALB - Yet Another List Builder (roster Editor for Wargames/tabletop games).
    Copyright (C) 2008-2012  Daniel Ricardo Castro Alvarado

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

#ifndef UNITWIDGET_H
#define UNITWIDGET_H

#include <QWidget>

#include "propertyform.h"
#include "unitoptions.h"
#include "categorylimitdialog.h"

#include "../collections/unitcollection.h"

namespace Ui {
    class UnitWidget;
}

class UnitWidget : public QWidget
{
    Q_OBJECT

public:
    UnitWidget(const UnitCollection& _units, ItemSelector& _itemSelector, CategoryLimitDialog& _categoryDialog, QWidget *parent = 0);
    ~UnitWidget();

    void clearUnitForm();

    QList<KeyPair<Unit> > getOrderedUnits();

public slots:
    void addUnit(const Unit &unit, const QString &key);
    void updateUnit(const QString& oldKey, const Unit& newUnit,
                    const QString& newKey);
    void removeUnit(const QString& key);

    void addUnitCategory(const GameCategory &category, const QString &key);
    void updateUnitCategory(const QString& oldKey, const GameCategory& newCategory,
                         const QString& newKey);
    void removeUnitCategory(const QString& key);

    void addOptionCategory(const GameCategory &category, const QString &key);
    void updateOptionCategory(const QString& oldKey, const GameCategory& newCategory,
                         const QString& newKey);
    void removeOptionCategory(const QString& key);

    void addOptionProperty(const GameProperty& property, const QString &key);
    void updateOptionProperty(const QString& oldKey, const GameProperty& newProperty,
                         const QString& newKey);
    void removeOptionProperty(const QString& key);

    void addUnitProperty(const GameProperty& property, const QString &key);
    void updateUnitProperty(const QString& oldKey, const GameProperty& newProperty,
                         const QString& newKey);
    void removeUnitProperty(const QString& key);

    void addColumn(const Column &column, const QString &key);
    void updateColumn(const QString& oldKey, const Column& newColumn,
                         const QString& newKey);
    void removeColumn(const QString& key);

    void clearAll();
    void clearAllColumns();
    void clearAllCategories();
    void clearAllProperties();

signals:
    void addUnitRequested(const Unit& unit, const QString& key);
    void updateUnitRequested(const QString& oldKey, const Unit& newUnit,
                                 const QString& newKey);
    void removeUnitRequested(const QString& key);

protected:
    void changeEvent(QEvent *event);

private slots:
    void on_unitTree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
    void on_unitAdd_clicked();
    void on_unitUpdate_clicked();
    void on_unitDelete_clicked();
    void on_unitClone_clicked();
    void on_unitSetItems_clicked();
    void on_unitSetProperties_clicked();
    void on_unitSetOptions_clicked();

    void on_unitTree_userChangedText(QTreeWidgetItem *item, int column);

    void on_unitMoveUp_clicked();

    void on_unitMoveDown_clicked();

    void on_unitItemLimitsButton_clicked();

private:
    Ui::UnitWidget *ui;

    PropertyForm propertyForm;

    const UnitCollection *units; // (Variable) pointer to const UnitCollection
    QHash<QString, QTreeWidgetItem*> keyIndex;

    QHash<QString, int> columnIndex;

    UnitOptions optionForm;

    ItemSelector *itemSelector;
    CategoryLimitDialog *categoryDialog;

    CategoryDataHash currentLimits;
};

inline void UnitWidget::addOptionProperty(const GameProperty &property, const QString &key) {
    optionForm.addProperty(property, key);
}

inline void UnitWidget::updateOptionProperty(const QString &oldKey, const GameProperty &newProperty,
                                         const QString &newKey) {
    optionForm.updateProperty(oldKey, newProperty, newKey);
}

inline void UnitWidget::removeOptionProperty(const QString &key) {
    optionForm.removeProperty(key);
}

inline void UnitWidget::addUnitProperty(const GameProperty &property, const QString &key) {
    propertyForm.addProperty(property, key);
}

inline void UnitWidget::updateUnitProperty(const QString &oldKey, const GameProperty &newProperty,
                                         const QString &newKey) {
    propertyForm.updateProperty(oldKey, newProperty, newKey);
}

inline void UnitWidget::removeUnitProperty(const QString &key) {
    propertyForm.removeProperty(key);
}

inline void UnitWidget::addOptionCategory(const GameCategory &category, const QString &key) {
    optionForm.addCategory(category, key);
}

inline void UnitWidget::updateOptionCategory(const QString& oldKey, const GameCategory& newCategory,
                     const QString& newKey) {
    optionForm.updateCategory(oldKey, newCategory, newKey);
}

inline void UnitWidget::removeOptionCategory(const QString& key) {
    optionForm.removeCategory(key);
}

#endif // UNITWIDGET_H
